﻿function doKeyUp(evt) {
    var input = document.getElementById("LostItemName").value;
    if (input == "") {
        evt = (evt) ? evt : ((window.event) ? event : null);
        if (evt) {
            keyCode = evt.keyCode ? evt.keyCode : evt.which;
            if (keyCode == 8) {
                showValue();
            }
        }
    }
}
function getStr(str, k) {
    $(document).ready(function () {
        $.ajax({
            url: '/LAF/LookupItem/',
            type: 'POST',
            data: 'name=' + str,
            dataType: 'json',
            success: function (result) {
                var itemID = result[0];
                var divList = document.getElementById("att");
                if (k == 0) {
                    var fieldset=document.getElementById(itemID);
                    divList.removeChild(fieldset);
                }
                else {
                    var fieldset = document.createElement("fieldset");
                    fieldset.id = itemID;
                    var legend = document.createElement("legend");
                    legend.innerHTML = str;
                    fieldset.appendChild(legend);
                    for (var i = 0; i < (result.length - 1) / 2; i++) {
                        var newAttribute = makeAttribute(itemID, result[i * 2 + 1], result[(i + 1) * 2]);
                        fieldset.appendChild(newAttribute);                        
                    }
                    divList.appendChild(fieldset);
                }
            }
        });
    });
}
function showValue() {
    var oldTag = document.getElementById("hiddenTag").value;
    var oldList = new Array();
    if (oldTag == "")
        oldList.length=0;
    else
        oldList = oldTag.split(",");
    var str = document.getElementsByName("hiddenTagList")[0].value;
    var strlist = new Array();
    if (str == "")
        strlist.length=0;
    else
        strlist = str.split(",");
    if (strlist.length > oldList.length) {
        getStr(strlist[strlist.length - 1], 1);
        oldList = strlist;
        oldTag = oldList[0];
        for (var i = 1; i < oldList.length; i++) {
            oldTag += "," + oldList[i];
        }
        document.getElementById("hiddenTag").value = oldTag;
        return;
    }
    else {
        for (var i = 0; i < oldList.length; i++) {
            if (strlist[i] != oldList[i]) {
                getStr(oldList[i], 0);
                oldList = strlist;
                if (oldList.length == 0)
                    oldTag = "";
                else {
                    oldTag = oldList[0];
                    for (var j = 1; j < oldList.length; j++) {
                        oldTag += "," + oldList[j];
                    }
                }
                    document.getElementById("hiddenTag").value = oldTag;
                return;
            }
        }
    }
}
function makeAttribute(itemID, attID, att) {    
    var attribute = document.createElement("div");
    attribute.className = "row";
    var span = document.createElement("span");
    span.className = "label_heading";
    var label = document.createElement("label");
    label.innerHTML = att;
    span.appendChild(label);
    attribute.appendChild(span);
    var input = document.createElement("input");
    input.type = "text";
    input.name = itemID +"a"+ attID;
    input.id = itemID + "attribute" + attID;
    input.className = itemID + "attribute" + attID;
    input.onfocus = function () {
        var id = "#" + itemID + "attribute" + attID;
        var source = "/LAF/AttributeTag?attID=" + attID;
        jQuery(id).tagsManager({
            typeahead: true,
            typeaheadAjaxPolling: true,
            typeaheadAjaxSource: source,
            validator: false
        });
    };
    attribute.appendChild(input);
    return attribute;
}
function tagAttribute(itemID, attID) {
    $(document).ready(function () {
        var id = "#" + itemID + "attribute" + attID;
        var source = "/LAF/AttributeTag?attID=" + attID;
        jQuery(id).tagsManager({
            typeahead: true,
            typeaheadAjaxPolling: true,
            typeaheadAjaxSource: source,
            validator: false
        });
    });
}